// https://leetcode.cn/problems/duplicate-zeros/description/


class Solution {
public:
    void duplicateZeros(vector<int>& arr) {
        int n = arr.size();
        int cnt = 0;
        int i = 0;
        for(; i < n && cnt < n; i++, cnt++) {
            if(arr[i] == 0) {
                cnt++;
            }
        }
        bool flag = false;
        if(cnt == n) {
            flag = true;
        }
        int k = i - 1;
        for(int j = k; j >= 0 && n >= 0; j--) {
            if(arr[j] || (j == k && !flag)) {
                if(n > 0)
                    arr[--n] = arr[j];
            }else {
                if(n > 0)
                    arr[--n] = 0;
                if(n > 0)
                    arr[--n] = 0;
            }
        }
    }
};